package com.lgd.factoryattendance.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lgd.factoryattendance.R;
import com.lgd.factoryattendance.Utils.ToastUtils;
import com.wdeo3601.pdfview.PDFView;

//  pdf 预览界面
public class PdfShowActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "PDF预览界面：";
    PDFView pdfView;
    LinearLayout layout_back;
    TextView tv_title, iv_currentPageIndex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdf_show);
        initView();
    }
    public void initView(){
        iv_currentPageIndex = findViewById(R.id.iv_currentPageIndex);
        tv_title = findViewById(R.id.tv_title);
        pdfView = findViewById(R.id.pdfView);
        layout_back = findViewById(R.id.layout_back);
        layout_back.setOnClickListener(this);

        Intent intent = getIntent();
        String path = intent.getStringExtra("pdf_path");
        Log.d(TAG, "initView:..........."+path);
        String pdf_name = intent.getStringExtra("pdf_name");

        PdfShowActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv_title.setText(pdf_name);
                pdfView.setOffscreenPageLimit(2);
                pdfView.isCanZoom(true);
                pdfView.setMaxScale(8f);
                // 设置当前页变化的回调监听
                pdfView.setOnPageChangedListener(new PDFView.OnPageChangedListener() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onPageChanged(int currentPageIndex, int totalPageCount) {
                        Log.d(TAG, "当前页数: "+ currentPageIndex +" 总页数: "+ totalPageCount);
                        iv_currentPageIndex.setText("当前页数："+ (currentPageIndex+1)+"/"+totalPageCount);
                    }
                });
                pdfView.showPdfFromUrl(path);
            }
        });
    }
    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.layout_back) {
            finish();
        }
    }

    // 设置 按两次返回键 退出app
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                ToastUtils.warnShowLong(getApplicationContext(),"再按一次退出PDF预览界面");
                exitTime = System.currentTimeMillis();
            } else {
                // 退出 销毁
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        recycleMemory();
    }

    //  回收 内存
    public void recycleMemory(){
        System.gc();
        Runtime.getRuntime().runFinalization();
        Log.d(TAG, "recycleMemory: 回收内存成功");
    }


}